package com.colin.commentservice.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.colin.bean.Comment;
import com.colin.commentservice.exception.sub.CommentNotExistException;
import com.colin.commentservice.exception.sub.VideoNotExistException;
import com.colin.commentservice.mapper.CommentMapper;
import com.colin.commentservice.service.CommentService;
import com.colin.response.R;
import com.colin.response.RCM;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private Snowflake snowflake;

    @Override
    public R<List<Comment>> getCommentById(Long videoId) {
        if (commentMapper.isExistVideo(videoId) == null) {
            throw new VideoNotExistException(RCM.VIDEO_NOT_EXIST.getMessage());
        }
        List<Comment> commentList = commentMapper.selectCommentById(videoId);
        return R.ok(commentList);
    }

    @Override
    public R<Boolean> addComment(Long videoId, Comment comment, String loginId) {
        //判断视频是否存在
        if (commentMapper.isExistVideo(videoId)==null) {
            return R.error(RCM.VIDEO_NOT_EXIST);
        }
        //封装要添加的评论属性
        comment.setVideoId(videoId);
        comment.setUserId(Long.valueOf(loginId));
        comment.setCreateTime(LocalDateTime.now());
        comment.setParentId(null);
        comment.setLikeCount(0L);
        comment.setStatus(0);
        //通过雪花算法生成全局id
        long id = snowflake.nextId();
        comment.setId(id);
        log.info("雪花算法生成id：{}", id);
        if (!commentMapper.addComment(comment)) {
            log.error("评论生成失败 视频id：{}", videoId);
        }
        return R.ok(true);
    }

    @Override
    public R<Boolean> deleteComment(Long commentId, String loginId) {
        log.info("删除评论id：{}", commentId);
        if (!commentMapper.deleteComment(commentId,loginId)) {
            throw new CommentNotExistException(RCM.COMMENT_NOT_EXIST.getMessage());
        }
        return R.ok(true);
    }

    @Override
    public R<Boolean> addChildComment(Long videoId, Long commentId, Comment comment, String loginId) {
        //判断视频是否存在
        if (commentMapper.isExistVideo(videoId)!=null) {
            return R.error(RCM.VIDEO_NOT_EXIST);
        }
        //封装评论属性
        comment.setVideoId(videoId);
        comment.setUserId(Long.valueOf(loginId));
        comment.setCreateTime(LocalDateTime.now());
        comment.setParentId(commentId);
        comment.setLikeCount(0L);
        comment.setStatus(0);
        //通过雪花算法生成全局id
        long id = snowflake.nextId();
        comment.setId(id);
        log.info("雪花算法生成id：{}", id);
        if (commentMapper.addComment(comment)) {
            log.error("评论生成失败 视频id：{}", videoId);
        }
        return R.ok(true);
    }

    @Override
    public R<Boolean> likeComment(Long videoId, Long commentId, String loginId) {
        //判断评论是否存在
        if (commentMapper.isCommentExist(commentId)==null) {
            return R.error(RCM.COMMENT_NOT_EXIST);
        }
        log.info("点赞评论id：{}", commentId);
        if (!commentMapper.addLikeComment(videoId, commentId, loginId ,LocalDateTime.now())) {
            log.error("评论点赞失败 评论id：{}", commentId);
        }
        commentMapper.updateLikeCount(commentId);
        return R.ok(true);
    }
}
